//
//  DateUtil.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/3/3.
//

import UIKit

class DateUtil {
    func getInternalBetweenDate(_ date: String,format: String = "MMM d, yyyy HH:mm:ss" ) -> String{
        if date == "" {
            return ""
        }
        //TimeZone for Server
        let sydneyTimeZone = TimeZone(identifier: "Australia/Sydney")
        let dateFormatter = DateFormatter()
        let locale = Locale(identifier: "en-au")//en是语言码，au是地区码，覆盖设备上语言和地区的设置
        dateFormatter.locale = locale
        dateFormatter.dateFormat =  format
        dateFormatter.timeZone = sydneyTimeZone
        
        if let convertDate = dateFormatter.date(from: date) {
            if let interval = sydneyTimeZone?.secondsFromGMT() {
                let fromDate = convertDate.addingTimeInterval(TimeInterval(interval))
                let toDate = Date().addingTimeInterval(TimeInterval(interval))
                let components = Calendar.current.dateComponents([.second,.minute,.hour,.day], from: fromDate, to: toDate)
                
                if let days = components.day {
                    if days > 1 {
                        return date
                    }else if days == 1{
                        return "Yesterday"
                    }else if days == 0 {
                        if let hours = components.hour {
                            if hours > 0 {
                                if hours == 1 {
                                    return "\(hours) hour ago"
                                }else{
                                    return "\(hours) hours ago"
                                }
                            }else {
                                if let mins = components.minute {
                                    if mins > 0 {
                                        return "\(mins) minutes ago"
                                    }else {//some seconds ago
                                        return "Just now"
                                    }
                                }
                            }
                        }
                    }
                }
            }
            
        }
        
        return date
    }
    //MARK: - 获取当前时间的标准格式
    func getCurrentDatetime() -> String{
        let nowDate = Date.current
        let dm: DateFormatter = DateFormatter()
        let locale = Locale(identifier: "en-au")//en是语言码，au是地区码，覆盖设备上语言和地区的设置
        dm.locale = locale
        dm.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" // "MMM dd YYYY HH:mm:ss.SSS"
        return dm.string(from: nowDate)
    }
    //MARK: - 获取当前时间戳,毫秒级别
    func getCurrentDatetimeTamp() -> String{
        let temp = Date().timeIntervalSince1970 * 1000
        let nowTamp = UInt64(temp)
        return "\(nowTamp)"
    }
    
    //MARK: - 获取日期的字符串
    func getDatetimeStr(_ date: Date? = nil, format: String = "yyyy-MM-dd'T'HH:mm:ss.sssZ",timeZone: TimeZone? = TimeZone(identifier: "Australia/Sydney")) -> String{
        let dealDate = date ?? Date()
        let dm: DateFormatter = DateFormatter()
        dm.dateFormat =  format
        dm.timeZone = timeZone
        return dm.string(from: dealDate)
    }
    //MARK: - 将日期字符串从format 改成new format
    func convertDateTimeStrToDate(_ dateTimeStr: String)-> Date?{
        var resultDate: Date?
        let dm: DateFormatter = DateFormatter()
        dm.locale = Locale(identifier: "en_AU")
        dm.dateFormat = "MMM dd yyyy HH:mm:ss.SSS"  // "MMM dd YYYY HH:mm:ss.SSS"
        if let date = dm.date(from: dateTimeStr){
            resultDate = date
        }else{
            dm.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.sssZ"
            if let date = dm.date(from: dateTimeStr){
                resultDate = date
            }
        }
        return resultDate
    }
    //MARK: - 将字符串转换为Date类型
    func convertStrToDate(dateStr: String,formatStr: String = "yyyy/MM/dd" ) -> Date?{
        let dateFormatter = DateFormatter()
        let locale = Locale(identifier: "en-au")//en是语言码，au是地区码，覆盖设备上语言和地区的设置
        dateFormatter.locale = locale
        dateFormatter.dateFormat = formatStr
        let date = dateFormatter.date(from: dateStr)
        return date
    }
    //MARK: - 将日期字符串转换为新format的字符串
    func convertDateStrToDateStr(dateStr: String,oldFormat: String,newFormat: String ) -> String{
        var resultStr = ""
        let dateFormatter = DateFormatter()
        let locale = Locale(identifier: "en-au")//en是语言码，au是地区码，覆盖设备上语言和地区的设置
        dateFormatter.locale = locale
        dateFormatter.dateFormat = oldFormat
        dateFormatter.pmSymbol = "PM"
        dateFormatter.amSymbol = "AM"
        if let date = dateFormatter.date(from: dateStr) {
            dateFormatter.dateFormat = newFormat
            resultStr = dateFormatter.string(from: date)
        }
        return resultStr
    }
    
    //日期距离现在是否超过三天
    func getInternalToNow(fromDate: Date)-> Bool{
        var updateFrequancy: Int = 1 //没设置就默认一天
        //版本更新频率
        if let days = UserDefaults.standard.object(forKey: "update_reminder_frequency") as? Int {
            updateFrequancy = days
        }
        //        let dateFormatter = DateFormatter()
        //        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        //        let fromStr = "2020-02-18 11:32:00"
        //        let fromDateTemp = dateFormatter.date(from: fromStr)
        //            // format the current date and time by the date formatter
        let nowDate = Date()
        //        let dateStr = dateFormatter.string(from: nowDate)
        //
        //        let temp = dateFormatter.string(from: fromDate)
        
        let components = Calendar.current.dateComponents([.day], from: fromDate, to: nowDate)
        if let day = components.day, day >= updateFrequancy  {
            return true
        }
        return false
    }
    //MARK: - 获取今年前活几年后的今天日期, 负数是几年前，整数是几年后
    func getServeralYearOfToday(date: Date, year: Int =  0, month: Int = 0,day: Int = 0, week: Int = 0)->Date{
        let calendar = Calendar.current
        var components = DateComponents()
        components.year = year
        components.month = month
        components.day = day
        components.weekOfYear = week
        //startOfCurrentMonth
        let today = calendar.dateComponents([.year, .month, .day, .weekOfYear], from: date)
        if let todayComponent = calendar.date(from: today),let lastYearDate = calendar.date(byAdding: components, to: todayComponent){
            return lastYearDate
        }
        
        return Date()
    }
    //获取某年某月有几天
    func howManyDaysOfMonth(inThisYear year: Int, withMonth month: Int) -> Int {
        if (month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12) {
            return 31
        }
        if (month == 4) || (month == 6) || (month == 9) || (month == 11) {
            return 30
        }
        if (year % 4 == 1) || (year % 4 == 2) || (year % 4 == 3) {
            return 28
        }
        if year % 400 == 0 {
            return 29
        }
        if year % 100 == 0 {
            return 28
        }
        return 29
    }
    //MARK: - 判断两个时间字符串是不是同一个时间,true表示一致，false表示不一致
//    func compareTwoDateTimeStr(_ dateTimeStr1: String, _ dateTimeStr2: String)->Bool{
//        var resultFlag: Bool = false
//        let dm: DateFormatter = DateFormatter()
//        dm.timeZone = TimeZone.current
//        dm.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
//        if let date1 = dm.date(from: dateTimeStr1), let date2 = dm.date(from: dateTimeStr2), date1 == date2{
//            resultFlag = true
//        }
//        return resultFlag
//    }
    //MARK: - 无用暂时屏蔽
//    func getNowDeemYears()->String {
//        let calendar = NSCalendar.current
//
//        //这里注意 swift要用[,]这样方式写
//        let currentYear = calendar.component(Calendar.Component.year, from: Date())
//        //2016's deem year is 15, 以后每年递减
//        return "\(15 - (currentYear - 2016))"
//    }
}
